package main

import (
	"context"
	"fmt"
	"io"
	"net/http"

	"gitee.com/ctyunfaas/cf-runtime-go-sdk/cf"
)

func HandleHttpRequest(ctx context.Context, w http.ResponseWriter, req *http.Request) error {
	body, err := io.ReadAll(req.Body)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		w.Header().Add("Content-Type", "text/plain")
		w.Write([]byte(err.Error()))
		return nil
	}
	w.WriteHeader(http.StatusOK)
	w.Header().Add("Content-Type", "text/plain")
	w.Write([]byte(fmt.Sprintf("hi %s! ~from ctyun FaaS", body)))
	return nil
}

func main() {
	cf.StartHttp(HandleHttpRequest)
}
